package com.cupster.func_db_litepal;

import android.app.Application;
import org.litepal.LitePal;
import org.litepal.LitePalDB;

public class LitePalManager {

    public static void init(Application application){
        LitePal.initialize(application);
    }

    public static void newDataBase(String dbName ,int version ,Class... tables){
        LitePalDB litePalDB = new LitePalDB(dbName ,version);
        for (Class table: tables){
            litePalDB.addClassName(table.getName());
        }
        LitePal.use(litePalDB);
    }

    public static void switchDbDefault(){
        LitePal.useDefault();
    }
    public static void switchDB(String dbName){
        LitePalDB litePalDB = LitePalDB.fromDefault(dbName);
        LitePal.use(litePalDB);
    }

    public static void deleteDB(String dbName){
        LitePal.deleteDatabase(dbName);
    }

    public static void beginTransaction(){
        LitePal.beginTransaction();
    }
    public static boolean excuteTransaction(boolean... flags){
        boolean success = true;
        for (boolean f : flags){
            if (!f){
                success = false;
                break;
            }
        }
        if (success){
            LitePal.setTransactionSuccessful();
        }
       endTransaction();
        return success;
    }

    public static void endTransaction(){
        LitePal.endTransaction();
    }
}
